<?php

/**
 *  @FileComment            [前台个人中心页控制器]
 *  @ProjectName             项目名称：ShopFrame
 *  @FileName                   文件名称：MemberController.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-4 21:39:57
 *  *
 */
class MemberController extends CommonController {

        public function index() {
                $order = K('order');
                $data = K('OrderOrderListGoodsView')->get_data(session('uid'), 'time', 2);
                $this->assign('data', $data);
                $this->orderstatu();
                $this->display();
        }

        public function myOrder() {
                if (IS_POST) {
                        $comment = K('comment');
                        $olid = Q('post.olid', 0, 'intval');
                        if ($comment->create()) {
                                $comment->add_data($olid);
                                $this->success('评论成功！');
                        } else {
                                $this->error($comment->error);
                        }
                }
                $act = Q('get.act', '');
                //获得当前用户所有订单ID
                $olids = K('OrderOrderListView')->get_field_data(session('uid'));
                $noComm = array();
                if (!empty($olids)) {
                        //如果用户购买过商品，则走这一步
                        $sum = count($olids);
                        $comed = K('OrderOrderListCommentView')->get_count_data(session('uid'));
                        $comeNum = count($comed);
                        $nowNum = $sum - $comeNum;
                        //未被评论的订单
                        $this->assign('nowNum', $nowNum);
                        if (!$comed) {
                                $comed = array();
                        }
                        //如果该数组中有已经评论过的订单ID，则剔除
                        foreach ($olids as $v) {
                                if (!in_array($v, $comed)) {
                                        $noComm[] = $v;
                                }
                        }
                        $this->assign('noComm', $noComm);
                }
                if (!$noComm) {
                        $noComm = array();
                }
                $noComm = implode(',', $noComm);
                switch ($act) {
                        case 'comment':
                                $data = K('OrderOrderListGoodsView')->get_all_data('user_uid=' . session('uid') . ' and olid in(' . $noComm . ')');
                                break;
                        case 'confirm':
                                $data = K('OrderOrderListGoodsView')->get_all_data('user_uid=' . session('uid') . ' and orderstatu="已发货"');
                                break;
                        case 'waitsend' :
                                $data = K('OrderOrderListGoodsView')->get_all_data('user_uid=' . session('uid') . ' and orderstatu="待发货"');
                                break;
                        case 'end' :
                                $data = K('OrderOrderListGoodsView')->get_all_data('user_uid=' . session('uid') . ' and orderstatu="已完成"');
                                break;
                        default:
                                $data = K('OrderOrderListGoodsView')->get_all_data('user_uid=' . session('uid'));
                                break;
                }
                $this->assign('data', $data);
                $this->orderstatu();
                $this->display();
        }

        private function orderstatu() {
                //订单状态信息
                //获得所有订单状态的订单数量
                $order = K('OrderOrderListGoodsView');
                $nopay = $order->get_statu_count_data('orderstatu = "' . '未付款"' . ' and user_uid = ' . session('uid'));
                $waitsend = $order->get_statu_count_data('orderstatu = "' . '待发货"' . ' and user_uid = ' . session('uid'));
                $havesend = $order->get_statu_count_data('orderstatu = "' . '已发货"' . ' and user_uid = ' . session('uid'));
                $end = $order->get_statu_count_data('orderstatu = "' . '已完成"' . ' and user_uid = ' . session('uid'));
                $this->assign('nopay', $nopay);
                $this->assign('waitsend', $waitsend);
                $this->assign('havesend', $havesend);
                $this->assign('end', $end);
        }

        public function send() {
                //付款
                $orderList = K('OrderList');
                $olid = Q('get.olid', 0, 'intval');
                $orderList->update_send_data($olid);
                $this->success('付款成功！');
        }

        public function set() {
                //修改个人信息
                $model = K('UserEdit');
                if (IS_POST) {
                        if ($model->create()) {
                                $model->edit_data(session('uid'));
                                $this->success('个人信息修改成功！');
                        } else {
                                $this->error($model->error);
                        }
                }
                $userInfo = $model->get_one_data(session('uid'));
                $this->assign('userInfo', $userInfo);
                $this->display();
        }

        public function comment() {
                //查看我的评论
                $data = K('OrderOrderListCommentView')->get_data('user_uid=' . session('uid'));
                $this->assign('data', $data);
                $this->display();
        }

}
